from flask import current_app
from qiniu import Auth, put_data

access_key = 'NKRYiXgall1sKsalpMhvuTVR0Kaw8WVrBwYGfpfu'
secret_key = 'PubP1ICGlhOjAqaBK5pQ_scvoxQwihiWWC8k3pNq'


def storage(data):
	try:
		# 构建鉴权对象
		q = Auth(access_key, secret_key)
	except Exception as e:
		current_app.logger.error(e)
		raise e

	# 要上传的空间
	bucket_name = 'music'

	# 生成上传 Token，可以指定过期时间等
	# 文件名可以不传，服务器会自动生成
	token = q.upload_token(bucket_name, None, 3600)

	ret, info = put_data(token, None, data)
	if info.status_code != 200:
		raise Exception('上传图片失败')
	print(info)

	return ret['key']

if __name__ == '__main__':
	file = input('请输入文件路径')
	with open(file, 'rb') as f:
		storage(f.read())
